<template>
  <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 10%; left: 0;"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'BarChart',
  data() {
    return {
      chart: null,
      option: {
        xAxis: {
          type: 'category',
          data: ['2013年', '2015年', '2016年', '2017年', '2019年', '2020年', '2021年']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [120, 200, 150, 80, 70, 110, 130],
            type: 'bar'
          }
        ]
      }
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });
      this.chart.setOption(this.option);
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize();
      }
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

#chart-container {
  position: relative;
  height: 100%;
  overflow: hidden;
}
</style>
